package com.Crawlzongheng;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.CrawlData.CrawlListPageBase;
import com.zonghengdb.ZongHengDB;
/**
 * 更新列表页信息采集基类
 * @author john
 *
 */
public class UpdateList extends CrawlListPageBase{
	//设置头信息头信息
	private static HashMap<String, String> params;
	static {
		params = new HashMap<String, String>();
		params.put("Referer", "http://book.zongheng.com");
		params.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36");
		params.put("Host", "book.zongheng.com");
	}
	
	public UpdateList(String pageUrl) {
		super(pageUrl, "utf-8",params);
	}
	
	@Override
	/**
	 * 下一跳URL的正则表达式
	 */
	public String getUrlRegexStr() {
		return "<a class=\"fs14\" href=\"(.*?)\"" ;
	}
	
	@Override
	/**
	 * 提取信息在正则表达式的位置
	 */
	public int getUrlRegexNum() {	
		return 1;
	}
	/**
	 * 获取更新列表页的URL
	 * @param exceptOther
	 * @return
	 */
	public List<String> getPageUrl(boolean exceptOther){
		List<String> urls = getPageUrl();
		if(exceptOther){
			List<String> excepUrls = new ArrayList<String>();
			for(String url: urls){
				if(url.indexOf("zongheng") > 0){
					excepUrls.add(url);
				}
			}
			return excepUrls;
			
		}
		return urls;
	}
	
	
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		UpdateList updateList =new UpdateList("http://book.zongheng.com/store.html");
		for(String s : updateList.getPageUrl(true)){
			System.out.println(s);
		}
		ZongHengDB db = new ZongHengDB();
		db.saveInfoUrls(updateList.getPageUrl(true));
	}
}
